'use strict';

const Controller = require('egg').Controller;
module.exports = app => {
  return class FrontArticleController extends Controller {
    async test() {}

    /**
     * @description 首页
     * query 的type是config.newlistTemplate对应的部分
     * @author xiezuobing[948466]<435321508@qq.com>
     * @memberof HomeController
     */
    async list() {
      const ctx = this.ctx;
      const query = ctx.query;
      const articleType = await this.service.articleType.selectByName(query.name);
      if (articleType) {
        //设置文章
        for (let type in articleType.children) {
          articleType.children[type].articles = await this.service.article.listAll(
            {
              type: articleType.children[type].id
            }
          )
        }
      }


      ctx.locals = {
        template: app.config.newlistTemplate.path + app.config.newlistTemplate[query.templateType || 1],
        templateType: query.templateType || 1,
        articleType: articleType,
        pagetitle: articleType && articleType.name
      }
      // ctx.body = articleType;
      await ctx.render('front/article/list.html');
    }

    /**
     * @description 详情
     * @author xiezuobing[948466]<435321508@qq.com>
     */
    async detail() {
      const ctx = this.ctx;
      const query = ctx.query;
      const article = await this.service.article.selectByIdWithRelative(query.articleId);
      if(article) {
        await this.service.operationLog.create(
          app.config.operationType.type.VIEW,
          app.config.operationType.target.ARTICLE,
          article.id,
          ctx.session.frontUser.user_id
        );
        if (article.relative_article) {
          article.rels = await this.service.article.listAll({
            id: {
              $in: article.relative_article.split(',')
            }
          }, false, null, '');
        }
        ctx.locals = {
          article: article,
          pagetitle: '文章详情 | ' + article.title
        }
      }
      
      // ctx.body = article;
      await ctx.render('front/article/detail.html');
    }

    async search() {
      const ctx = this.ctx;
      ctx.locals = {
        pagetitle: '搜索 | ' + ctx.query.keyword
      }
      await ctx.render('front/search.html');
    }

    //json
    async searchJson() {
      const ctx = this.ctx;
      const query = ctx.query;
      let params = {
        keyword: query.keyword,
        pageNo: query.pageNo,
      }
      let where = {
        $or: [
          {
            title: {
              $like: '%' + params.keyword + '%'
            }
          },
          {
            keywords: {
              $like: '%' + params.keyword + '%'
            }
          },
          {
            author: {
              $like: '%' + params.keyword + '%'
            }
          }
        ],
        publish: 1,
      };
      try {
        const data = await this.service.article.list(
          params.pageNo,
          app.config.pageBean.pageSize,
          'publish_time',
          'DESC',
          where,
          ['id', 'title']
        );
        ctx.body = data;
      } catch (e) {
        ctx.body = ErrorUtil.UNKOWN;
      }
    }
  }
}
